package com.community.app.controller;


import com.community.body.domain.ZyComplaintSuggest;
import com.community.body.domain.ZyFiles;
import com.community.body.domain.ZyVisitor;
import com.community.body.dto.ComplaintSuggestDTO;
import com.community.body.dto.OwnerDTO;
import com.community.body.dto.VisitorDTO;
import com.community.body.service.IZyComplaintSuggestService;
import com.community.body.service.IZyFilesService;
import com.community.body.service.IZyOwnerService;
import com.community.body.service.IZyVisitorService;
import com.community.common.core.domain.AjaxResult;
import com.community.common.utils.file.FileUtils;
import com.community.common.utils.oss.AliyunOssUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/mini/community/wx/visitor")
public class WxVisitorController {

    @Autowired
    private IZyVisitorService visitorService;
    @Autowired
    private IZyOwnerService zyOwnerService;
    @Autowired
    private IZyComplaintSuggestService zyComplaintSuggestService;
    @Resource
    private IZyFilesService zyFilesService;
    @Resource
    private RedisTemplate<String, Long> redisTemplate;

    /**
     * 查询访客列表
     *
     * @param visitorDTO
     * @return
     */
    @RequestMapping("selectVisitorList")
    public AjaxResult selectVisitorList(VisitorDTO visitorDTO) {
        List<VisitorDTO> list = visitorService.selectZyVisitorList(visitorDTO);
        return AjaxResult.success(list);
    }

    /**
     * 添加访客
     *
     * @param zyVisitor
     * @return
     */
    @RequestMapping("insertVisitor")
    public AjaxResult insertZyVisitor(@RequestBody ZyVisitor zyVisitor) {
        int i = visitorService.insertZyVisitor(zyVisitor);
        return i > 0 ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    /**
     * 查询业主列表
     */
    @GetMapping("ownerList")
    public AjaxResult list(OwnerDTO ownerDTO) {
        List<OwnerDTO> list = zyOwnerService.selectZyOwnerList(ownerDTO);
        return AjaxResult.success(list);
    }

    /**
     * 添加投诉建议
     */
    @PostMapping("suggest")
    public AjaxResult add(@RequestBody ZyComplaintSuggest zyComplaintSuggest) {
        int i = zyComplaintSuggestService.insertZyComplaintSuggest(zyComplaintSuggest);
        return i > 0 ? AjaxResult.success("投诉成功！") : AjaxResult.error("投诉失败！");
    }

    /**
     * 查询投诉列表
     */
    @GetMapping("selectSuggestion")
    public AjaxResult selectSuggestion(ComplaintSuggestDTO complaintSuggestDTO) {
        System.err.println(complaintSuggestDTO.getStatus());
        List<ComplaintSuggestDTO> list = zyComplaintSuggestService.selectZyComplaintSuggestList(complaintSuggestDTO);
        return AjaxResult.success(list);
    }

    /**
     * 投诉建议上传图片
     *
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("suggestUpload")
    public AjaxResult suggestUpload(MultipartFile file, HttpServletRequest request) throws Exception {
        Long owner = Long.valueOf(request.getHeader("owner"));
        if (!file.isEmpty()) {
            String url = AliyunOssUploadUtils.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", FileUtils.getName(url));
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            ZyFiles zyFiles = new ZyFiles();
            zyFiles.setFilesUrl(url);
            zyFiles.setDelFlag(0L);
            zyFiles.setCreateTime(new Date());
            zyFiles.setUserId(owner);
            zyFiles.setSource(0L);
            ValueOperations<String, Long> value = redisTemplate.opsForValue();
            Long s = value.get("complaintSuggestId");
            zyFiles.setParentId(s);
            zyFiles.setRemark("ComplaintSuggest");
            zyFilesService.insertZyFiles(zyFiles);
            return AjaxResult.success("上传成功", ajax);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }


    /**
     * 删除建议与投诉
     *
     * @param complaintSuggestId
     * @return
     */
    @RequestMapping("delSuggest")
    public AjaxResult delSuggest(Long complaintSuggestId) {
        int i = zyComplaintSuggestService.deleteZyComplaintSuggestByComplaintSuggestId(complaintSuggestId);
        return i > 0  ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
